package service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ConsoleService 
{
	private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	
	public String getType()
	{
		String result = "";
		
		try
		{
			System.out.print("Enter type (values: CHEAP, USUAL, EXPENSIVE, HOT): ");
			result = br.readLine().split(" ")[0];
		}
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
	    }
		
		return result;
	}
	
	public String getSurname()
	{
		String result = "";
		
		try
		{
			System.out.print("Enter surname: ");
			result = br.readLine().split(" ")[0];
			
			if (result.isEmpty())
				throw new Exception("Surname can not be empty!");
		}
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
	    }
		
		return result;
	}
	
	public String getSurnameWithExit()
	{
		String result = "";
		
		try
		{
			System.out.print("Enter surname: ");
			result = br.readLine().split(" ")[0];
			
			if (result.isEmpty())
				throw new Exception("Surname can not be empty!");
		}
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
			System.exit(1);
	    }
		
		return result;
	}
	
	public String getHotel()
	{
		String result = "";
		
		try
		{
			System.out.print("Enter hotel: ");
			result = br.readLine().split(" ")[0];
			
			if (result.isEmpty())
				throw new Exception("Hotel can not be empty!");
		}
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
			System.exit(1);
	    }
		
		return result;
	}
	
	public Date getDate()
	{
		Date result = null;
		
		try
		{
			System.out.print("Enter date (format: yyyy.MM.dd): ");
			String tmpStr = br.readLine().split(" ")[0];
			
			SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
			format.setLenient(false);
			result = format.parse(tmpStr);
		}
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
		}
		
		return result;
	}
	
	public String getDateAsString()
	{
		String result = "";
		
		try
		{
			System.out.print("Enter date (format: yyyy.MM.dd): ");
			result = br.readLine().split(" ")[0];
		}
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
			System.exit(1);
		}
		
		return result;
	}
	
	public int getIndexToRemove()
	{
		int result = -1;
		
		try
		{
			System.out.print("Enter number of tour to remove: ");
			String tmpStr = br.readLine().split(" ")[0];
			
			result = Integer.parseInt(tmpStr);
		}
		catch (Exception e) 
		{
			result = -1;
	    }
		
		return result;
	}
	
	public int getMonth()
	{
		int result = -1;
		
		try
		{
			System.out.print("Enter month for group report (1 - 12): ");
			String tmpStr = br.readLine().split(" ")[0];
			
			result = Integer.parseInt(tmpStr);
		}
		catch (Exception e) 
		{
			result = -1;
	    }
		
		return result;
	}
	
	public boolean isAddNewTour()
	{
		boolean result = false;
		
		try
		{
			System.out.print("Tour with such client not found!\nDo You want add new tour for this client [Y/N]: ");
			String tmpStr = br.readLine().split(" ")[0];
			
			if (tmpStr.compareToIgnoreCase("y") == 0)
				result = true;
		}
		catch (Exception e) 
		{
			result = false;
	    }
		
		return result;
	}
	
	public void delay()
	{
		try 
		{
			System.out.print("Press any key...");
			System.in.read();
		}
		catch (IOException e) 
		{
			System.out.println(e.getMessage());
		}
	}

}
